package com.dongdong.android.framework.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dongdong.android.framework.R;
import com.dongdong.android.framework.dialog.LoadingDialog;
import com.dongdong.android.framework.mvp.BaseModel;
import com.dongdong.android.framework.mvp.BasePresenter;
import com.dongdong.android.framework.utils.NetWorkUtil;
import com.dongdong.android.framework.utils.TUtil;
import com.dongdong.android.framework.utils.ToastUtil;

/**
 * Created by dongdong on 2018/3/27.
 */
public abstract class BaseFragment<T extends BasePresenter, E extends BaseModel> extends Fragment {

    protected T mPresenter;
    protected E mModel;
    protected Context mAppContext;
    protected Context mContext;
    private View mRootView;
    private IntentFilter mIntentFilter;
    public LoadingDialog mDialog;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mRootView == null) {
            mRootView = inflater.inflate(getLayoutId(), container, false);
        }

        mPresenter = TUtil.getT(this, 0);
        mModel = TUtil.getT(this, 1);

        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }

        mAppContext = BaseApplication.getAppContext();
        mContext = this.getContext();

        initView();
        initData();
        initListener();
        initPresenter();


        return mRootView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        onAttachToContext(context);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            onAttachToContext(activity);
        }

    }

    public void onAttachToContext(Context context) {
        if (null == mIntentFilter) {
            mIntentFilter = new IntentFilter();
        }
        wrapBroadcastReceiver(mIntentFilter);
        LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(receiver, mIntentFilter);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(receiver);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(receiver);

    }


    public abstract int getLayoutId();

    public abstract void initView();

    public abstract void initData();

    public abstract void initListener();

    /**
     * 简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
     */
    public abstract void initPresenter();


    /**
     * 用于注册广播 添加Action
     */
    public abstract void wrapBroadcastReceiver(IntentFilter filter);


    /**
     * 收到广播的回调
     *
     * @param intent 广播的Intent
     */
    protected void dealBroadCastReceiver(Intent intent) {

    }

    final BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            dealBroadCastReceiver(intent);
        }
    };


    /**
     * show the progress dialog
     */
    public void showProgressDialog() {
        if (NetWorkUtil.isConnectingToInternet()) {
            if (mDialog == null) {
                mDialog = new LoadingDialog(mContext);
            }
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.setCancelable(true);

            if (mDialog.isShowing() == false) {

                mDialog.show();
            }
        } else {
            ToastUtil.showLongToast(mContext, R.string.common_dialog_no_network);
        }
    }

    /**
     * Dismiss the progress dialog
     */
    public void dismissProgressDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

}
